﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace BDAccessSharingUniqueEventReceiver
{
    class ExceptionHandler
    {
        public void SetErrorLog(string title, string ErrMsg, SPItemEventProperties properties)
        {
            try
            {
                using (SPSite osite = new SPSite(properties.OpenSite().Url))
                {
                    using (SPWeb oweb = osite.OpenWeb())
                    {
                        string ListName = "Exception Logs";
                        //if (ListExists(oweb, ListName) == true)
                        //{
                        //    SPList olist = oweb.Lists[ListName];
                        AcceptException(ListName, title, ErrMsg, properties);
                        //}
                        //else
                        //{
                        //    CreateList(ListName, title, ErrMsg);
                        //}
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public bool ListExists(SPWeb oweb, string listname)
        {
            var lists = oweb.Lists;
            foreach (SPList olist in lists)
            {
                if (olist.Title.Equals(listname))
                    return true;
            }
            return false;
        }
        public void CreateList(string ListName, string title, string ErrMsg, SPItemEventProperties properties)
        {
            try
            {
                using (SPSite osite = new SPSite(properties.OpenSite().Url))
                {
                    using (SPWeb oweb = osite.RootWeb)
                    {
                        oweb.AllowUnsafeUpdates = true;
                        oweb.Lists.Add(ListName, null, SPListTemplateType.GenericList);
                        SPList olist = oweb.Lists[ListName];
                        olist.Fields.Add("ErrorMessage", SPFieldType.Note, true);
                        AcceptException(ListName, title, ErrMsg, properties);
                        oweb.AllowUnsafeUpdates = false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;

            }
        }
        public void AcceptException(string ListName, string title, string ErrMsg, SPItemEventProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite osite = new SPSite(properties.OpenSite().Url))
                {
                    using (SPWeb oweb = osite.RootWeb)
                    {
                        oweb.AllowUnsafeUpdates = true;
                        SPList olist = oweb.Lists[ListName];
                        SPListItem oitm = olist.Items.Add();


                        oitm["Title"] = title;
                        oitm["ErrorMessage"] = ErrMsg;
                        oitm.Update();
                        oweb.AllowUnsafeUpdates = false;
                    }
                }
            });
        }
    }
}
